<template>
  <!--
    成绩 功能列表

    @author wh ${authorEmail}
    @date 2021-09-06 22:52:32
  -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生" prop="studentId">
        <el-select v-model="queryParams.studentId" ref="queryParams.studentId" size="small">
          <el-option v-for="item in LearnStudentList"
                     :key="item.id"
                     :label="item.studentName"
                     :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程" prop="courseId">
        <el-select v-model="queryParams.courseId" ref="queryParams.courseId" size="small">
          <el-option v-for="item in LearnCourseList"
                     :key="item.id"
                     :label="item.courseName"
                     :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="任课教师" prop="teacherId">
        <el-select v-model="queryParams.teacherId" ref="queryParams.teacherId" size="small">
          <el-option v-for="item in LearnTeacherList"
                     :key="item.id"
                     :label="item.teacherName"
                     :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="成绩" prop="grade">
        <el-input
          v-model="queryParams.grade"
          placeholder="请输入成绩"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="LearnGradeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学生" align="left" prop="studentId">
        <template slot-scope="scope">
          <span>{{getLearnStudentName(scope.row.studentId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程" align="left" prop="courseId">
        <template slot-scope="scope">
          <span>{{getLearnCourseName(scope.row.courseId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任课教师" align="left" prop="teacherId">
        <template slot-scope="scope">
          <span>{{getLearnTeacherName(scope.row.teacherId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="成绩" align="left" prop="grade" />

<!--      <el-table-column label="状态" prop="hasStatus">-->
<!--        <template slot-scope="scope">-->
<!--        <el-switch-->
<!--            v-model="scope.row.hasStatus"-->
<!--            :active-value="0"-->
<!--            :inactive-value="1"-->
<!--            @change="handleStatusChange(scope.row)"-->
<!--          ></el-switch>-->
<!--         </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="添加时间" align="center" prop="addTime">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseDatetime(scope.row.addTime) }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改成绩对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生" prop="studentId">
          <el-select v-model="form.studentId" ref="form.studentId" size="small">
            <el-option v-for="item in LearnStudentList"
                       :key="item.id"
                       :label="item.studentName"
                       :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程" prop="courseId">
          <el-select v-model="form.courseId" ref="form.courseId" size="small">
            <el-option v-for="item in LearnCourseList"
                       :key="item.id"
                       :label="item.courseName"
                       :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任课教师" prop="teacherId">
          <el-select v-model="form.teacherId" ref="queryParams.teacherId" size="small">
            <el-option v-for="item in LearnTeacherList"
                       :key="item.id"
                       :label="item.teacherName"
                       :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="成绩" prop="grade">
          <el-input v-model="form.grade" placeholder="请输入成绩" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLearnGrade,
  getLearnGrade,
  delLearnGrade,
  addLearnGrade,
  changeLearnGradeField,
  changeStatusLearnGrade,
  updateLearnGrade,
  exportLearnGrade } from "@/api/student/LearnGrade";

import {
  listLearnStudent } from "@/api/student/LearnStudent";

import {
  listLearnCourse } from "@/api/student/LearnCourse";

import {
  listLearnTeacher } from "@/api/student/LearnTeacher";

import  Condition  from "alinesno-ui/packages/Search/Condition"
import {listLearnCollege} from "@/api/student/LearnCollege";
export default {
  name: "LearnGrade",
  components: {
  },
  data() {
    return {
      LearnCourseList:[],
      searchCourseParams:[],
      LearnStudentList:[],
      searchStudentParams:[],
      LearnTeacherList:[],
      searchTeacherParams:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 状态
      statusOptions:[] ,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 成绩表格数据
      LearnGradeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
        courseId: null,
        teacherId: null,
        grade: null
      },
      // 查询参数配置对象
      queryParamsConfig: {
        studentId: Condition.like(),
        courseId: Condition.like(),
        teacherId: Condition.like(),
        grade: Condition.like(),
      },
      // 搜索参数
      searchParams:[] ,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studentId: [
          { required: true, message: "学生不能为空", trigger: "blur" }
        ],
        courseId: [
          { required: true, message: "课程不能为空", trigger: "blur" }
        ],
        teacherId: [
          { required: true, message: "班级不能为空", trigger: "blur" }
        ],
        grade: [
          { required: true, message: "成绩不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.searchTeacherParams = this.searchParam(null, {pageSize: 10});
    this.searchCourseParams = this.searchParam(null, {pageSize: 10});
    this.searchStudentParams = this.searchParam(null, {pageSize: 10});
    this.handleQuery() //解决刷新界面不显示内容

    // 查询公共状态
    this.getDicts("has_status").then(response => {
      this.statusOptions = response.data;
    });

  },
  methods: {
    /** 查询成绩列表 */
    getList() {
      this.loading = true;

      listLearnTeacher(this.searchTeacherParams).then(response => {
        this.LearnTeacherList = response.rows;
      });

      listLearnCourse(this.searchCourseParams).then(response => {
        this.LearnCourseList = response.rows;
      });

      listLearnStudent(this.searchStudentParams).then(response => {
        this.LearnStudentList = response.rows;
      });

      listLearnGrade(this.searchParams).then(response => {
        this.LearnGradeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        studentId: null,
        courseId: null,
        teacherId: null,
        grade: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // 获取参数
      this.searchParams = this.searchParam(this.queryParamsConfig, this.queryParams);

      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加成绩";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const studentId = row.id || this.ids
      getLearnGrade(studentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改成绩";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLearnGrade(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLearnGrade(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const studentIds = row.id || this.ids;
      this.$confirm('是否确认删除成绩编号为"' + studentIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLearnGrade(studentIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 状态修改 **/
    handleStatusChange(row) {
      return changeStatusLearnGrade(row.id, row.status).then(response=>{
        if(response.code == 200){
          this.msgSuccess("操作成功");
        }
      });
    },
    /** 修改字段状态 **/
    chanageFile(value , filed , id){
      return changeLearnGradeField(value , filed , id).then(response =>{
        if(response.code == 200){
          this.msgSuccess("操作成功");
        }
      }) ;
    } ,
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有成绩数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportLearnGrade(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    getLearnStudentName(id) {
      for(let i = 0;i < this.LearnStudentList.length;i++) {
        if(this.LearnStudentList[i].id.toString()===id) {
          return this.LearnStudentList[i].studentName;
        }
      }
      return "";
    },
    getLearnTeacherName(id) {
      for(let i = 0;i < this.LearnTeacherList.length;i++) {
        if(this.LearnTeacherList[i].id.toString()===id) {
          return this.LearnTeacherList[i].teacherName;
        }
      }
      return "";
    },
    getLearnCourseName(id) {
      for(let i = 0;i < this.LearnCourseList.length;i++) {
        if(this.LearnCourseList[i].id.toString()===id) {
          return this.LearnCourseList[i].courseName;
        }
      }
      return "";
    }
  }
};
</script>
